package com.sxt.sys.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sxt.sys.constast.SYSConstast;
import com.sxt.sys.domain.User;
import com.sxt.sys.service.UserService;
import com.sxt.sys.utils.DataGridView;
import com.sxt.sys.utils.HanyuPinyinHelperUtils;
import com.sxt.sys.utils.WebUtils;
import com.sxt.sys.vo.UserVo;

@Controller
@RequestMapping("user")
public class UserController {

	@Autowired
	private UserService userService;
	
	//跳到用户管理的界面
	@RequestMapping("toUserManager")
	public String toUserManager(){
		return "system/userManager";
	}
	
	//查询
	@RequestMapping("loadAllUser")
	@ResponseBody
	public DataGridView loadAllUser(UserVo userVo){
		return this.userService.queryAllUser(userVo);
	}
	
	//添加
	@RequestMapping("addUser")
	@ResponseBody
	public Map<String,Object> addUser(UserVo userVo){
		Map<String,Object> map=new HashMap<>();
		try {
			//设置用户类型
			userVo.setType(SYSConstast.USER_TYPE_NORMAL);
			userVo.setPwd(SYSConstast.USER_DEFAULT_PWD);
			this.userService.addUser(userVo);
			map.put("msg", "添加成功");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("msg", "添加失败");
		}
		return map;	
	}
	
	//修改用户
	@RequestMapping("updateUser")
	@ResponseBody
	public Map<String,Object> updateUser(UserVo userVo){
		Map<String,Object> map=new HashMap<>();
		try {
			this.userService.updateUser(userVo);
			map.put("msg", "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("msg", "修改失败");
		}
		return map;
	}
	
	//删除用户
		@RequestMapping("deleteUser")
		@ResponseBody
		public Map<String,Object> deleteUser(UserVo userVo){
			Map<String,Object> map=new HashMap<>();
			try {
				this.userService.deleteUser(userVo);
				map.put("msg", "删除成功");
			} catch (Exception e) {
				e.printStackTrace();
				map.put("msg", "删除失败");
			}
			return map;
		}
		
		/**
		 * 把真实名称转成拼音
		 */
		@RequestMapping("getPinyinName")
		@ResponseBody
		public String getPinyinName(UserVo userVo){
			return HanyuPinyinHelperUtils.toHanyuPinyin(userVo.getRealname());
		}
		
		/**
		 * 重置密码
		 */
		@RequestMapping("resetUserPwd")
		@ResponseBody
		public Map<String,Object> resetUserPwd(UserVo userVo){
			Map<String,Object> map=new HashMap<>();
			try {
				userVo.setPwd(SYSConstast.USER_DEFAULT_PWD);
				this.userService.updateUser(userVo);
				map.put("msg", "重置成功");
			} catch (Exception e) {
				e.printStackTrace();
				map.put("msg", "重置失败");
			}
			return map;
		}
		/**
		 * 根据用户ID加载角色列表并选中当前用户已拥有的角色
		 */
		@RequestMapping("loadUserRolesByUserId")
		@ResponseBody
		public DataGridView loadUserRolesByUserId(UserVo userVo){
			return this.userService.queryUserRolesByUserId(userVo.getUserid());
		}
		
	    /**
	     * 保存角色和用户的关系
	     */
		@RequestMapping("saveUserRoles")
		@ResponseBody
		public Map<String,Object> saveUserRoles(UserVo userVo){
			Map<String,Object> map=new HashMap<>();
			try {
				this.userService.saveUserRoles(userVo);
				map.put("msg", "分配成功");
			} catch (Exception e) {
				e.printStackTrace();
				map.put("msg", "分配失败");
			}
			return map;
		}
		
		
		
		//----------------------------------修改密码
		@RequestMapping("toUpdatePwd")
		public String toUpdatePwd(){
			return "system/userPwdUpdateManager";
			
		}
		
		//保存新密码
		@RequestMapping("updateUserPwd")
		@ResponseBody
		public Map<String,Object> updateUserPwd(UserVo userVo){
			Map<String,Object> map=new HashMap<>();
			try {
				User user = WebUtils.getCurrentUser();
				userVo.setUserid(user.getUserid());
				this.userService.updateUser(userVo);
				map.put("msg", "改密成功");
			} catch (Exception e) {
				e.printStackTrace();
				map.put("msg", "改密失败");
			}
			return map;
		}
		
}

